<title>网站用户</title>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>帮助与反馈</cite></a>
    <a><cite>常见问题</cite></a>
  </div>
</div>

<div class="layui-fluid">

  <div class="layui-card">
    <div class="layui-card-body">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-userfront-formlist">
        <div style="padding-bottom: 10px;">
          <button class="layui-btn layuiadmin-btn-useradmin" data-type="add"> 添加</button>
        </div>

        <table class="layui-hide" id="table-instance" lay-filter="table-instance"></table>

        <script type="text/html" id="table-useradmin-admin">
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-4"></i>删除</a>
        </script>
      </div>
    </div>
  </div>

  <script>
    layui.use('useradmin', layui.factory('useradmin')).use(['admin', 'useradmin', 'table'], function () {
      var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , setter = layui.setter
        , table = layui.table
        , form = layui.form;

      form.render(null, 'layadmin-userfront-formlist');
      //监听搜索
      form.on('submit(LAY-user-front-search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('table-instance', {
          where: field
        });
      });

      //事件
      var active = {
        add: function () {
          admin.popup({
            title: '添加常见问题'
            , area: ['700px', '450px']
            , id: 'LAY-popup-useradmin-add'
            , success: function (layero, index) {
              view(this.id).render('question/form').done(function () {
                form.render(null, 'layuiadmin-form-admin');
                //监听提交
                form.on('submit(LAY-user-back-submit)', function (data) {
                  var field = data.field; //获取提交的字段
                  //提交 Ajax 成功后，关闭当前弹层并重载表格
                  //$.ajax({});
                  editquestionHandle(Object.assign(data, field));
                  layui.table.reload('LAY-user-front-submit'); //重载表格
                  layer.close(index); //执行关闭 
                });
              });
            }
          });
        }
      };

      table.render({
        elem: "#table-instance",
        url: setter.request.baseUrl + "question/list",
        method: "get",
        headers: { token: layui.data("layuiAdmin").token },
        response: {
          statusName: "code",
          statusCode: 0,
          msgName: "msg",
          countName: "totalCount",
          dataName: "question",
        },
        cols: [
          [
            { field: "title", title: "标题" },
            { field: "content", title: "内容" },
            { fixed: "right", title: "操作", toolbar: "#table-useradmin-admin" },
          ],
        ],
        page: true,

      });
      table.on("tool(table-instance)", function (obj) {
        var data = obj.data;
        if (obj.event === "del") {
          layer.confirm("真的删除么", function (index) {
            delquestionHandle({
              questionId: data.questionId,
            });
            obj.del();
            layer.close(index);
          })
        } else {
          admin.popup({
            title: "编辑热门问题",
            area: ['700px', '450px'],
            success: function (layero, index) {
              view(this.id)
                .render("question/form", data)
                .done(function () {
                  let _data = data;
                  console.log(_data)

                  form.render(null, "layuiadmin-form-user");
                  form.on('submit(LAY-user-back-submit)', function (data) {
                    var field = data.field;
                    // warning
                    editquestionHandle(Object.assign(_data, field));
                    // layui.table.reload('LAY-user-back-manage'); //重载表格
                    layer.close(index);
                  });
                });
            },
          });
        }
      });

      //修改编辑
      function editquestionHandle(params) {
        admin.req({
          url: setter.request.baseUrl + "question/save",
          data: JSON.stringify(params),
          type: "post",
          dataType: "json"
          , contentType: "application/json",
          done: function (res) {
            layui.table.reload("table-instance");
          },
        });
      }
      //删除
      function delquestionHandle(params) {
        console.log('params+' + params);
        admin.req({
          url: setter.request.baseUrl + "question/delete",
          data: {
            ...params,
          },
          type: "post",
          done: function (res) {
            layer.msg(res.msg);
          },
        });
      }
      $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
      });
    });
  </script>